
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument

'// TODO
args = inputbox("board/ver/name/nameb",args,"board")
args = lcase(trim(args))
if len(args) =0 then exit sub

select case args
	case "board" 
		GetBoardSize
	case "ver" 
		GetVerInfo
	case "name"
		GetPCBName
	case "nameb" 
		GetPCBNameOnBoard	
	case "tpp" 
		GetTestPointInfo(true)
	case "bpp" 
		GetTestPointInfo(false)
	case "part" 
		GetAllPartInfo
	case "cell" 
		GetAllCellInfo
	case "drcnet" 
		GetDrcNetInfo
end select 	
set doc = nothing


sub GetDrcNetInfo
	dim infile:infile = doc.path & "LogFiles\Proximity.txt"
	dim outfile:outfile = doc.path & "output\Proximity-Net.txt"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objInput = objFSO.OpenTextFile(infile, 1)  ' 1=只读模式
	Set objOutput = objFSO.CreateTextFile(outfile, True) ' True=覆盖已存在文件
	
	Set regEx = CreateObject("VBScript.RegExp")
	regEx.Pattern = "Net Name:\s+(\w+)\s+.*?Net Name:\s+(\w+)" ' 匹配两组网络名
	regEx.IgnoreCase = True
	regEx.Global = True

	Do Until objInput.AtEndOfStream
		strLine = objInput.ReadLine
		Set matches = regEx.Execute(strLine)
		
		If matches.Count > 0 Then
			For Each match In matches
				' 提取两个网络名并格式化
				strResult = match.SubMatches(0) & "  " & match.SubMatches(1)
				objOutput.WriteLine strResult
			Next
		End If
	Loop

	objInput.Close
	objOutput.Close

	call openfile(outfile)
end sub
sub GetAllCellInfo
	doc.unhighlightall()
	doc.currentunit = 4 'mm
	dim prts,prt
	set prts = doc.cells("*")
	dim ostr:ostr = ""
	prts.sort
	for each prt in prts
		if ostr<>"" then ostr = ostr & vbCrlf
		ostr = ostr & prt.name & chr(9) & prt.components.count 
	next
	
	dim ofile:ofile = doc.path & "output\cell.txt"
	dim fso,fw
	set fso = createobject("scripting.filesystemobject")
	set fw = fso.createTextfile(ofile)
	fw.write(ostr)
	fw.close
	set fw = nothing
	set fso = nothing
	
	dim wsh
	set wsh =createobject("wscript.shell")
	wsh.run(ofile)
	set wsh = nothing	
end sub
sub GetAllPartInfo
	doc.unhighlightall()
	doc.currentunit = 4 'mm
	dim prts,prt
	set prts = doc.parts("*")
	dim ostr:ostr = ""
	prts.sort
	
	dim cmp,cmps
	dim cmpcell
	dim msg
	for each prt in prts
		on error resume next
		set cmps = prt.components
		for each cmp in cmps
			msg=""
			msg = cmp.FindProperty("Description").value
			exit for
		next
	
		if msg<>"" then
			if ostr<>"" then ostr = ostr & vbCrlf
			ostr = ostr & prt.name & chr(9) & msg & chr(9) & prt.components.count 	
			if prt.components.count=1 then prt.highlighted = true
		end if		
	next
	
	dim ofile:ofile = doc.path & "output\part.txt"
	dim fso,fw
	set fso = createobject("scripting.filesystemobject")
	set fw = fso.createTextfile(ofile)
	fw.write(ostr)
	fw.close
	set fw = nothing
	set fso = nothing
	
	dim wsh
	set wsh =createobject("wscript.shell")
	wsh.run(ofile)
	set wsh = nothing	
end sub
sub GetTestPointInfo(obool)
	on error resume next
	dim cside:cside = 1
	if obool = false then cside = 512
	
	dim ostr:ostr =""
	dim output:output = doc.path & "output\tpp.txt"
	dim cmp,cmps
	set cmps = doc.components(0,-1,-1,"TP*")	
	dim cmppin,cmppins
	dim pinnet
	for each cmp in cmps
		if cmp.side = cside then
			pinnet ="None"
			set cmppins = cmp.pins("*")
			for each cmppin in cmppins
				pinnet = cmppin.net.name
				exit for
			next
			dim tstr :tstr = cmp.name & chr(9) & pinnet & chr(9)& cmp.positionX & chr(9) & cmp.positionY
			if ostr<>"" then ostr = ostr & vbcrlf
			ostr=ostr & tstr
		end if
	next

	dim fso:set fso = createobject("scripting.filesystemobject")
	dim fw:set fw = fso.createtextfile(output,1)
	fw.write(ostr)
	fw.close
	set fw = nothing
	set fso = nothing
	
	dim wsh:set wsh = createobject("wscript.shell")
	wsh.run(output)
	set wsh = nothing
end sub
sub GetBoardSize
	doc.unit=4
	Dim bds,bd
	Dim board_l,board_w
	board_l = doc.BoardOutline.Extrema.MaxX - doc.BoardOutline.Extrema.MinX
	board_w = doc.BoardOutline.Extrema.MaxY - doc.BoardOutline.Extrema.MinY 
	
	Dim Form, TextBox
	Set Form = CreateObject("Forms.Form.1") 
	Set TextBox = Form.Controls.Add("Forms.TextBox.1").Object 
	TextBox.MultiLine = True 
	TextBox.Text = board_l & "*" & board_w & "(mm)"
	TextBox.SelStart = 0 
	TextBox.SelLength = TextBox.TextLength 	
	TextBox.Copy
	msgbox "done"
	'msgbox board_l & "*" & board_w & "(mm)"
end sub
sub GetPCBName
	dim pcbname
	pcbname = left(doc.name,len(doc.name)-4)
	call SetClipboardText_fm20(pcbname)
	msgbox "done"
end sub
sub GetPCBNameOnBoard
	dim pcbname
	pcbname = left(doc.name,len(doc.name)-4)
	
	dim pcbnameonboard
	pcbnameonboard = ""
	dim s,ls,i
	s = split(pcbname,"-")
	ls = ubound(s)-lbound(s)+1
	for i=0 to ls-2
		pcbnameonboard = pcbnameonboard & s(i) & "-"
	next
	pcbnameonboard = pcbnameonboard & "B"	
	
	call SetClipboardText(pcbnameonboard)
	msgbox "done"
end sub
sub GetVerInfo
	dim pcbname
	pcbname = left(doc.name,len(doc.name)-4)

	
	dim pcbver
	dim s,ls,i
	s = split(pcbname,"-")
	ls = ubound(s)-lbound(s)+1
	pcbver = replace(date(),"/","-")
	dim ts,lts
	ts = split(pcbver,"-")
	lts = ubound(ts)-lbound(ts)+1
	for i=0 to lts-1
		if ts(i)<>"" then
			if len(ts(i))= 1 then
				ts(i) = "0" & ts(i)
			end if
		end if
	next
	pcbver = ""
	for i=0 to lts-1
		if ts(i)<>"" then
			pcbver = pcbver & ts(i) & "-"
		end if
	next
	pcbver = left(pcbver,len(pcbver)-1)
	pcbver = pcbver & "(" & s(ls-1) & ")"		
	call SetClipboardText_fm20(pcbver)
	msgbox "done"
end sub
